package com.itheima.algorithm.binarysearch;


import java.util.Iterator;
import java.util.function.Consumer;

/**
 * @author: TylerZhong
 * @description: 单向链表
 */
public class SignlyLinkedList implements Iterable<Integer> { // 整体
    private Node head = null; // 头指针

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head;
            @Override
            public boolean hasNext() { // 是否有下一个元素
                return p != null;
            }

            @Override
            public Integer next() { // 返回当前值，并指向下一个元素
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    private static class Node {
        int value; // 值
        Node next; // 下一个节点指针

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    public void addFirst(int value) {
        // 1. 链表为空
//        head = new Node(value, null);
        // 2. 链表非空
        head = new Node(value, head);
    }

    public Node findLast(){
        if (head == null) {
            return null;
        }
        Node p;
        for (p = head; p.next != null; p = p.next) {

        }
        return p;
    }

    public void addLast(int value) {
        Node last = findLast();
        if (last == null) {
            addFirst(value);
            return;
        }
        last.next = new Node(value, null);
    }

    private Node findNode(int index){
        Node p;
        int i = 0;
        for (p = head; p.next != null; p = p.next, i++) {
            if (index == i) {
                return p;
            }
        }
        return null;
    }

    /**
     * 根据索引查找
     * @param index - 索引
     * @return
     * 找不到，抛出 index 非法异常
     */
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw illegalArgumentException(index);
        }
        return node.value;
    }

    /**
     * 向索引位置插入
     * @param index - 索引
     * @param value - 待插入值
     * throws IllegalArgumentException - 找不到，抛出 index 非法异常
     */
    public void insert(int index, int value) throws IllegalArgumentException {
        if (index == 0) {
            addFirst(value);
            return;
        }
        Node pre = findNode(index - 1);
        if (pre == null) {
            throw illegalArgumentException(index);
        }
        pre.next = new Node(value, pre.next);
    }

    private static IllegalArgumentException illegalArgumentException(int index) {
        return new IllegalArgumentException(String.format("index [%d] 不合法%n", index));
    }

    /**
     * 删除第一个
     * 如果不存在，抛出index非法异常
     */
    public void removeFirst(){
        if (head == null) {
            throw illegalArgumentException(0);
        }
        head = head.next;
    }

    public void remove(int index){
        if (index == 0) {
            removeFirst();
            return;
        }
        Node pre = findNode(index-1);
        if (pre == null) {
            throw illegalArgumentException(index);
        }
        Node removed = pre.next;
        if (removed == null) {
            throw illegalArgumentException(index);
        }
        pre.next = removed.next;
    }

    public void loop(Consumer<Integer> consumer){
        Node p = head;
        while (p != null) {
            consumer.accept(p.value);
            p = p.next;
        }
    }

    public void loop2(Consumer<Integer> consumer) {
        for (Node p = head; p != null; p = p.next) {
            consumer.accept(p.value);
        }
    }

    /**
     * 递归遍历
     * @param before
     * @param after
     */
    public void loop3(Consumer<Integer> before, Consumer<Integer> after) {
        recursion(head,before,after);
    }

    private void recursion(Node curr, Consumer<Integer> before, Consumer<Integer> after){ // 某个节点要进行的操作
        if (curr == null) {
            return;
        }
        before.accept(curr.value);
        recursion(curr.next, before, after);
        after.accept(curr.value);
    }
}
